﻿
@{
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

<body>
    @using yiran.Areas.Admin.Models.Entity
    @model sys_user
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                @Html.AntiForgeryToken()

                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>登录账号
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="username" value="" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>登录密码
                    </label>
                    <div class="layui-input-inline">
                        <input type="password" id="L_pass" name="password" value="" required="" lay-verify="pass"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>确认密码
                    </label>
                    <div class="layui-input-inline">
                        <input type="password" id="L_repass" name="repwd" value="" required="" lay-verify="repass"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>昵称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="name" value="" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        角色
                    </label>
                    <div class="layui-input-block">

                        @foreach (var r in ViewData["role"] as List<sys_role>)
                        {
                            <input type="checkbox" name="role_id" value="@r.id" title="@r.name">
                        }
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="username" class="layui-form-label">
                        备注
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="remark" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label">
                    </label>
                    <button class="layui-btn" lay-filter="add" lay-submit="">
                        添加
                    </button>
                </div>
            </form>
        </div>
    </div>
    <script>
        layui.use(['form', 'layer'],
            function () {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer;
                //自定义验证规则
                form.verify({
                    pass: [/(.+){6,12}$/, '密码必须6到12位'],
                    repass: function (value) {
                        if ($('#L_pass').val() != $('#L_repass').val()) {
                            return '两次密码不一致';
                        }
                    }
                });
                //监听提交
                form.on('submit(add)',
                    function (data) {
                        var fdata = data.field;
                        fdata["Static"] = 1;
                        fdata["role_id"] = "";
                        $("input[name = role_id]:checked").each(function () {
                            fdata["role_id"] += $(this).val() + ","
                        })
                        fdata["role_id"] = fdata["role_id"].substring(0, fdata["role_id"].length - 1)
                        $.ajax({
                            url: '/Admin/Role/Useradd',
                            type: 'POST',
                            data: fdata,
                            dataType: 'json',
                            async: true,
                            success: function (data, status, xhr) {
                                if (data.code == 200) {
                                    layer.msg(data.result, {
                                        icon: 1,
                                        time: 2000
                                    }, function () {
                                        //关闭当前frame
                                        xadmin.close();

                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                                } else {
                                    layer.msg(data.result, { icon: 2 });
                                }
                            },
                            error: function () {
                                layer.msg('出错了~', { icon: 2 });
                            }
                        });
                        return false;
                    });
            });
    </script>
</body>
